package cn.agiledata.bank.information.service;

import java.sql.Clob;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.lob.SerializableClob;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.hibernateBean.Information;
import cn.agiledata.bank.information.dao.InformationDaoImpl;
import cn.agiledata.bank.information.form.FinanceForm;
import cn.agiledata.bank.information.form.InformationForm;

public class InformationServiceImpl implements IInformationService {
	private InformationDaoImpl informationDao;

	private String stringLength;

	private String listLength;

	private String perspectiveLength;
	
	public String getPerspectiveLength() {
		return perspectiveLength;
	}

	public void setPerspectiveLength(String perspectiveLength) {
		this.perspectiveLength = perspectiveLength;
	}

	public String getListLength() {
		return listLength;
	}

	public void setListLength(String listLength) {
		this.listLength = listLength;
	}

	public String getStringLength() {
		return stringLength;
	}

	public void setStringLength(String stringLength) {
		this.stringLength = stringLength;
	}

	public InformationDaoImpl getInformationDao() {
		return informationDao;
	}

	public void setInformationDao(InformationDaoImpl informationDao) {
		this.informationDao = informationDao;
	}

	public List getInformationByKind(String kind) {
		return informationDao.getInformationByKind(kind);
	}

	public Information getInformationById(String id) {
		return informationDao.getInformationById(id);
	}

	public List getInformationByKindDate(String kind, Date now) {
		return informationDao.getInformationByKindDate(kind, now);
	}

	public List getInformationByKindOccDate(String kind) {
		return informationDao.getInformationByKindOccDate(kind);
	}

	public List getInformationByType(String type) {
		return informationDao.getInformationByType(type);
	}

	public void setForm(InformationForm iform) {
		Date now = new Date();
		// zln 修正 20080402，取消支付信息，财经咨询修改为公告信息
		// 公告信息
		List informationListFinance = getInformationByKindDate(Constant.INFORMATION_NOTICE, now);
		// 公司动态
		List informationListCompany = getInformationByKindOccDate(Constant.INFORMATION_COMPANY);
		
		// 取得公告信息，最新5条
		if (informationListFinance.size() > 6) {
			informationListFinance = informationListFinance.subList(0, 5);
		}
		iform.setInformationFinance(informationListFinance);
		
		// 取得公司动态信息，最新5条
		if (informationListCompany.size() > 5) {
			informationListCompany = informationListCompany.subList(0, 4);
		}
		
		// 截取显示标题长度，以便于格式控制
		for (Iterator iter = informationListCompany.iterator(); iter.hasNext();) {
			Information info = (Information) iter.next();
			String strTitle = info.getTitle().trim();
			strTitle = StringUtil.subString(strTitle, 28);
			info.setDisplayTitle(strTitle);
		}
		
		iform.setInformationCompany(informationListCompany);
	}

	public void setFinanceForm(FinanceForm form) {
		List RoledDevelope = getInformatinByTwoType(Constant.FINANCE_ROLED,Constant.FINANCE_DEVELOPE);
		List FeaturePerspective = getInformatinByTwoType(Constant.FINANCE_FEATURE,Constant.FINANCE_PERSPECTIVE);
		Information firstFinance = new Information();
		String info = getInformationStr(RoledDevelope);// 财经时讯
		if (info != null) {
			form.setFirstFinanceRolede((Information) RoledDevelope.get(0));
			form.setFinanceRoled(RoledDevelope);
			form.setRoled(info);
		} else {
			firstFinance.setTitle("财经时讯没有信息");
			form.setFirstFinanceRolede(firstFinance);
		}

		info = getInformationStr(FeaturePerspective);// 专题
		if (info != null) {
			form.setFirstFinanceFeature((Information) FeaturePerspective.get(0));
			form.setFinanceFeature(FeaturePerspective);
			form.setFeature(info);
		} else {
			firstFinance.setTitle("专题没有信息");
			form.setFirstFinanceFeature(firstFinance);
		}

		int size = Integer.parseInt(listLength);
		//int perSize = Integer.parseInt(perspectiveLength);
		if (RoledDevelope.size() > size) {
			RoledDevelope = RoledDevelope.subList(0, size);
		} else if (RoledDevelope.size() != 0) {
			
		}
		if (FeaturePerspective.size() > size) {
			FeaturePerspective = FeaturePerspective.subList(0, size);
		} else if (FeaturePerspective.size() != 0) {
			
		}
		
		form.setFinanceDevelope(RoledDevelope);
		form.setFinancePerspective(FeaturePerspective);
	}

	private String getInformationStr(List informationList) {
		Information firstInformation = new Information();
		StringBuffer buffer;
		String info = "";

		if (!informationList.isEmpty()) {
			firstInformation = (Information) informationList.get(0);
			
			if (null != firstInformation.getIcontent() && !firstInformation.getIcontent().equals("")) {
				SerializableClob sc = (SerializableClob) firstInformation.getIcontent();

				Clob wrapclob = sc.getWrappedClob();
				if (wrapclob instanceof Clob) {
					Clob clob = (Clob) wrapclob;
					int length;
					String content = "";
					try {
						length = (int) clob.length();
						content = clob.getSubString(1, length);
					} catch (SQLException e) {
						e.printStackTrace();
					}

					info = StringUtil.replaceHtml(content);
					info = StringUtil.replace(info, "&nbsp;", "");
				}
				buffer = new StringBuffer(info);
			} else {
				buffer = new StringBuffer("尚未写入任何内容");
			}
			int length = Integer.parseInt(stringLength);
			if (buffer.length() > length) {
				info = buffer.substring(0, length) + "...";
			} else {
				info = buffer.toString();
			}
		} else {
			info = null;
		}
		return info;
	}

	public List getInformationPick(String pick) {
		List list = informationDao.getInformationPick(pick);
		if (list.size() > 10) {
			list = list.subList(0, 10);
		}
		return list;
	}

	public List getInformatinByTwoType(String type1, String type2) {
		
		return informationDao.getInformatinByTwoType(type1, type2);
	}

}
